package cn.yzgy.yzgyoa.ucenter.service.impl;

import cn.yzgy.yzgyoa.ucenter.entity.SysOrganization;
import cn.yzgy.yzgyoa.ucenter.mapper.SysOrganizationMapper;
import cn.yzgy.yzgyoa.ucenter.service.ISysOrganizationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 组织机构表
支持公司多级结构 服务实现类
 * </p>
 *
 * @author Yqsen
 * @since 2019-08-27
 */
@Service
public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationMapper, SysOrganization> implements ISysOrganizationService {

    @Override
    public IPage<SysOrganization> queryAll(IPage<SysOrganization> ipage, QueryWrapper<SysOrganization> wrapper) {
        return baseMapper.selectPage(ipage,wrapper);
    }

    @Override
    public int changeInfo(SysOrganization sysOrganization) {
        UpdateWrapper<SysOrganization> wrapper = new UpdateWrapper<>();
        wrapper.eq("oid",sysOrganization.getId());
        return baseMapper.update(sysOrganization,wrapper);
    }

    @Override
    public int updatePrimaryKey(Long newId, Long oid) {
        return baseMapper.updatePrimaryKey(newId,oid);
    }

    @Override
    public int delData(String oid) {
        return baseMapper.delete(new UpdateWrapper<SysOrganization>()
                .lambda().likeRight(SysOrganization::getId,oid));
    }

    @Override
    public int check(SysOrganization sysOrganization) {
        int num=1;
        QueryWrapper<SysOrganization> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_oid",sysOrganization.getParentId());
        wrapper.eq("name", sysOrganization.getName());
        if (baseMapper.check(wrapper) == null) {
            num = 0;
        }
        return num;
    }

    @Override
    public String getParentEnAbbr(SysOrganization sysOrganization) {
        QueryWrapper<SysOrganization> wrapper = new QueryWrapper<>();
        wrapper.eq("oid",sysOrganization.getParentId());
        return baseMapper.getEnAbbr(wrapper);
    }

    @Override
    public String getNewid(SysOrganization sysOrganization) {
        QueryWrapper<SysOrganization> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_oid",sysOrganization.getParentId());
        return baseMapper.getNewid(wrapper);
    }

    @Override
    public int addSysOrganization(SysOrganization sysOrganization) {
        return  baseMapper.insert(sysOrganization);
    }

    @Override
    public String getEnAbbr(String oid) {
        QueryWrapper<SysOrganization> wrapper = new QueryWrapper<>();
        wrapper.eq("oid",oid);
        return baseMapper.getEnAbbr(wrapper);
    }

    @Override
    public List<SysOrganization> getSysOrganizationList(String enAbbr) {
        return baseMapper.sysOrganizationList(enAbbr);
    }

}
